package cn.fanyamin.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import cn.fanyamin.domain.User;



public class UserDao extends BaseDao {
   public UserDao() {
   }
   
   public User get(String username)
      throws DaoException
   {
      try {
         begin();
         Query q = getSession().createQuery("from User where userName = :username");
         q.setString("userName",username);
         User user = (User)q.uniqueResult();
         commit();
         return user;
      } catch( HibernateException e ) {
         rollback();
         throw new DaoException("Could not get user " + username,e);
      }
   }
   
   public User create(String username,String password)
      throws DaoException
   {
      try {
         begin();
         User user = new User(username,password);
         getSession().save(user);
         commit();
         return user;
      } catch( HibernateException e ) {
         rollback();
         throw new DaoException("Could not create user " + username,e);
      }
   }
   
   public void delete(User user)
      throws DaoException
   {
      try {
         begin();
         getSession().delete(user);
         commit();
      } catch( HibernateException e ) {
         rollback();
         throw new DaoException("Could not delete user " + user.getUserName(),e);
      }
   }
}
